<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="跳转地址" prop="url">
        <el-input v-model="dataForm.url" :disabled="disabled" placeholder="跳转地址"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <!--        <el-input v-model="dataForm.type" :disabled="disabled" placeholder="类型，0为首页底部菜单，1为个人中心订单"></el-input>-->
        <el-select v-model="dataForm.type" clearable placeholder="活动类型">
          <el-option
            v-for="item in [{value:1,label:'个人中心栏目'},{value:0,label:'首页栏目'}]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="父级id" prop="parentId">
        <el-select :disabled="disabled" v-model="dataForm.parentId" clearable placeholder="父级选项">
          <el-option
            v-for="item in parentData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
<!--        <el-input v-model="dataForm.parentId" :disabled="disabled" placeholder="父级id"></el-input>-->
      </el-form-item>
      <el-form-item label="图片地址" prop="imgUrl">
        <el-input v-model="dataForm.imgUrl" :disabled="disabled" placeholder="图片地址"></el-input>
        <el-img v-model="dataForm.imgUrl" :disabled="disabled">
        </el-img>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" :disabled="disabled" placeholder="从大到小排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      disabled: false,
      visible: false,
      parentData: [],
      dataForm: {
        id: 0,
        url: '',
        type: '',
        sort: '',
        parentId: '0',
        imgUrl: '',
        name: ''},
      dataRule: {
        name: [
          {required: true, message: '名称不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (id, disabled) {
      this.disabled = disabled
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: `/platform/column/info/${this.dataForm.id}`,
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.column
            }
          })
        }
      })

      this.$nextTick(() => {
          this.$http({
            url: `/platform/column/queryAll`,
            method: 'POST',
            params: {
              'parentId': '0'
            }
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.parentData = data.list
              let parent={id:0,name:0}
              this.parentData.push(parent)
            }
          })
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm']
        .validate((valid) => {
          if (valid) {
            this.$http({
              url: `/platform/column/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
    }
  }
}
</script>
